#!/bin/sh -e
# This file is dedicated to the public domain.

case "`uname -s`" in
	# weird people using Windows Bash might type ./compile, help them out :)
	*NT*) # msys2 or busybox-w32
		echo "You're on Windows, idiot! Running compile.bat for you.">&2
		exec cmd /c compile.bat ;;
esac
case "`uname -r`" in
	*Microsoft*)
		echo "NOTE: building inside WSL. Use compile.bat to build for Windows!">&2
esac

mkdir -p .build/include

: "${CC:=clang --target=-i686-pc-linux-gnu}"
: "${HOSTCC:=clang}"

warnings="-Wall -pedantic -Wno-parentheses -Wno-missing-braces \
-Wno-gnu-zero-variadic-macro-arguments -Werror=implicit-function-declaration \
-Werror=vla"

stdflags="-std=c2x -D_DEFAULT_SOURCE -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64"

dbg=0
if [ "$dbg" = 1 ]; then
	cflags="-O0 -g3 -fsanitize-trap=undefined -DSST_DBG"
	ldflags="-O0 -g3"
else
	cflags="-O2 -fvisibility=hidden"
	ldflags="-O2 -s"
fi

objs=
cc() {
	_bn="`basename "$1"`"
	objs="$objs .build/${_bn%%.c}.o"
	_mn=" -DMODULE_NAME=${_bn%%.c}"
	# ugly annoying special case
	if [ "$_mn" = " -DMODULE_NAME=con_" ]; then _mn=" -DMODULE_NAME=con"
	elif [ "$_mn" = "-DMODULE_NAME=sst" ]; then _mn=; fi
	$CC -c -flto -fpic -fno-ident $cflags $warnings -I.build/include \
			$stdflags$_mn -o ".build/${_bn%%.c}.o" "src/$1"
}

ld() {
	$CC -shared -flto -fpic -fuse-ld=lld $ldflags -L.build -ldl \
			-ltier0 -lvstdlib -o .build/sst.so$objs
	mv .build/sst.so sst.so
}

src="\
	ac.c
	alias.c
	autojump.c
	bind.c
	chatrate.c
	chunklets/fastspin.c
	chunklets/msg.c
	clientcon.c
	con_.c
	crypto.c
	democustom.c
	demorec.c
	engineapi.c
	ent.c
	errmsg.c
	extmalloc.c
	fastfwd.c
	fixes.c
	fov.c
	gameinfo.c
	gameserver.c
	hexcolour.c
	hook.c
	hud.c
	inputhud.c
	kvsys.c
	l4daddon.c
	l4dmm.c
	l4dreset.c
	l4dwarp.c
	nosleep.c
	os.c
	portalcolours.c
	sst.c
	trace.c
	x86.c
	xhair.c"
if [ "$dbg" = 1 ]; then src="$src \
	dbg.c
	udis86.c"
fi

$HOSTCC -O2 -fuse-ld=lld $warnings $stdflags \
		-o .build/gluegen src/build/gluegen .c src/build/cmeta.c src/os.c
$HOSTCC -O2 -fuse-ld=lld $warnings $stdflags \
		-o .build/mkgamedata src/build/mkgamedata.c src/os.c
$HOSTCC -O2 -fuse-ld=lld $warnings $stdflags \
		-o .build/mkentprops src/build/mkentprops.c src/os.c
.build/gluegen `for s in $src; do echo "src/$s"; done`
.build/mkgamedata gamedata/engine.txt gamedata/gamelib.txt gamedata/inputsystem.txt \
gamedata/matchmaking.txt gamedata/vgui2.txt gamedata/vguimatsurface.txt
.build/mkentprops gamedata/entprops.txt
for s in $src; do cc "$s"; done
$CC -shared -fpic -fuse-ld=lld -O0 -w -o .build/libtier0.so src/stubs/tier0.c
$CC -shared -fpic -fuse-ld=lld -O0 -w -o .build/libvstdlib.so src/stubs/vstdlib.c
ld

$HOSTCC -O2 -g3 $warnings $stdflags -include test/test.h -o .build/bitbuf.test test/bitbuf.test.c
.build/bitbuf.test
# XXX: skipping this test on linux for now but should enable when we can test it
#$HOSTCC -m32 -O2 -g3 -include test/test.h -o .build/hook.test test/hook.test.c
#.build/hook.test
$HOSTCC -O2 -g3 $warnings $stdflags -include test/test.h -o .build/kv.test test/kv.test.c
.build/kv.test
$HOSTCC -O2 -g3 $warnings $stdflags -include test/test.h -o .build/x86.test test/x86.test.c
.build/x86.test

# vi: sw=4 tw=4 noet tw=80 cc=80
